{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "import json\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>money</th>\n",
       "      <th>open_interest</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:01</th>\n",
       "      <td>3350.0</td>\n",
       "      <td>3342.0</td>\n",
       "      <td>3350.0</td>\n",
       "      <td>3334.0</td>\n",
       "      <td>45292</td>\n",
       "      <td>1.512974e+09</td>\n",
       "      <td>2842292</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:02</th>\n",
       "      <td>3341.0</td>\n",
       "      <td>3343.0</td>\n",
       "      <td>3347.0</td>\n",
       "      <td>3341.0</td>\n",
       "      <td>33174</td>\n",
       "      <td>1.109131e+09</td>\n",
       "      <td>2836054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:03</th>\n",
       "      <td>3343.0</td>\n",
       "      <td>3345.0</td>\n",
       "      <td>3346.0</td>\n",
       "      <td>3340.0</td>\n",
       "      <td>15492</td>\n",
       "      <td>5.178647e+08</td>\n",
       "      <td>2834260</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:04</th>\n",
       "      <td>3345.0</td>\n",
       "      <td>3348.0</td>\n",
       "      <td>3352.0</td>\n",
       "      <td>3345.0</td>\n",
       "      <td>25934</td>\n",
       "      <td>8.686266e+08</td>\n",
       "      <td>2838874</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:05</th>\n",
       "      <td>3349.0</td>\n",
       "      <td>3349.0</td>\n",
       "      <td>3350.0</td>\n",
       "      <td>3346.0</td>\n",
       "      <td>17556</td>\n",
       "      <td>5.877604e+08</td>\n",
       "      <td>2839668</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    open   close    high     low  volume         money  \\\n",
       "date                                                                     \n",
       "2019-09-02 09:01  3350.0  3342.0  3350.0  3334.0   45292  1.512974e+09   \n",
       "2019-09-02 09:02  3341.0  3343.0  3347.0  3341.0   33174  1.109131e+09   \n",
       "2019-09-02 09:03  3343.0  3345.0  3346.0  3340.0   15492  5.178647e+08   \n",
       "2019-09-02 09:04  3345.0  3348.0  3352.0  3345.0   25934  8.686266e+08   \n",
       "2019-09-02 09:05  3349.0  3349.0  3350.0  3346.0   17556  5.877604e+08   \n",
       "\n",
       "                  open_interest  \n",
       "date                             \n",
       "2019-09-02 09:01        2842292  \n",
       "2019-09-02 09:02        2836054  \n",
       "2019-09-02 09:03        2834260  \n",
       "2019-09-02 09:04        2838874  \n",
       "2019-09-02 09:05        2839668  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "all_data = pd.read_csv(\"RB9999_1m_20190901_20210901.csv\", index_col=\"date\")\n",
    "all_data = all_data.iloc[:, 1:]\n",
    "all_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_data[\"ret_rate\"] = (all_data[\"close\"] - all_data.shift(2)[\"close\"]) / all_data.shift(2)[\"close\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>money</th>\n",
       "      <th>open_interest</th>\n",
       "      <th>ret_rate</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:01</th>\n",
       "      <td>3350.0</td>\n",
       "      <td>3342.0</td>\n",
       "      <td>3350.0</td>\n",
       "      <td>3334.0</td>\n",
       "      <td>45292</td>\n",
       "      <td>1.512974e+09</td>\n",
       "      <td>2842292</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:02</th>\n",
       "      <td>3341.0</td>\n",
       "      <td>3343.0</td>\n",
       "      <td>3347.0</td>\n",
       "      <td>3341.0</td>\n",
       "      <td>33174</td>\n",
       "      <td>1.109131e+09</td>\n",
       "      <td>2836054</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:03</th>\n",
       "      <td>3343.0</td>\n",
       "      <td>3345.0</td>\n",
       "      <td>3346.0</td>\n",
       "      <td>3340.0</td>\n",
       "      <td>15492</td>\n",
       "      <td>5.178647e+08</td>\n",
       "      <td>2834260</td>\n",
       "      <td>0.000898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:04</th>\n",
       "      <td>3345.0</td>\n",
       "      <td>3348.0</td>\n",
       "      <td>3352.0</td>\n",
       "      <td>3345.0</td>\n",
       "      <td>25934</td>\n",
       "      <td>8.686266e+08</td>\n",
       "      <td>2838874</td>\n",
       "      <td>0.001496</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:05</th>\n",
       "      <td>3349.0</td>\n",
       "      <td>3349.0</td>\n",
       "      <td>3350.0</td>\n",
       "      <td>3346.0</td>\n",
       "      <td>17556</td>\n",
       "      <td>5.877604e+08</td>\n",
       "      <td>2839668</td>\n",
       "      <td>0.001196</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    open   close    high     low  volume         money  \\\n",
       "date                                                                     \n",
       "2019-09-02 09:01  3350.0  3342.0  3350.0  3334.0   45292  1.512974e+09   \n",
       "2019-09-02 09:02  3341.0  3343.0  3347.0  3341.0   33174  1.109131e+09   \n",
       "2019-09-02 09:03  3343.0  3345.0  3346.0  3340.0   15492  5.178647e+08   \n",
       "2019-09-02 09:04  3345.0  3348.0  3352.0  3345.0   25934  8.686266e+08   \n",
       "2019-09-02 09:05  3349.0  3349.0  3350.0  3346.0   17556  5.877604e+08   \n",
       "\n",
       "                  open_interest  ret_rate  \n",
       "date                                       \n",
       "2019-09-02 09:01        2842292       NaN  \n",
       "2019-09-02 09:02        2836054       NaN  \n",
       "2019-09-02 09:03        2834260  0.000898  \n",
       "2019-09-02 09:04        2838874  0.001496  \n",
       "2019-09-02 09:05        2839668  0.001196  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"settings.json\") as f:\n",
    "    settings_dict = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'factor_span': 2, 'file_path': 'RB9999_1m_20190901_20210901.csv'}\n"
     ]
    }
   ],
   "source": [
    "def get_data(**d):\n",
    "    print(d)\n",
    "get_data(**settings_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "settings_dict[\"factor_span\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(settings_dict[\"factor_span\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>money</th>\n",
       "      <th>open_interest</th>\n",
       "      <th>ret_rate</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:01</th>\n",
       "      <td>3350.0</td>\n",
       "      <td>3342.0</td>\n",
       "      <td>3350.0</td>\n",
       "      <td>3334.0</td>\n",
       "      <td>45292</td>\n",
       "      <td>1.512974e+09</td>\n",
       "      <td>2842292</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:02</th>\n",
       "      <td>3341.0</td>\n",
       "      <td>3343.0</td>\n",
       "      <td>3347.0</td>\n",
       "      <td>3341.0</td>\n",
       "      <td>33174</td>\n",
       "      <td>1.109131e+09</td>\n",
       "      <td>2836054</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:03</th>\n",
       "      <td>3343.0</td>\n",
       "      <td>3345.0</td>\n",
       "      <td>3346.0</td>\n",
       "      <td>3340.0</td>\n",
       "      <td>15492</td>\n",
       "      <td>5.178647e+08</td>\n",
       "      <td>2834260</td>\n",
       "      <td>0.000898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:04</th>\n",
       "      <td>3345.0</td>\n",
       "      <td>3348.0</td>\n",
       "      <td>3352.0</td>\n",
       "      <td>3345.0</td>\n",
       "      <td>25934</td>\n",
       "      <td>8.686266e+08</td>\n",
       "      <td>2838874</td>\n",
       "      <td>0.001496</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:05</th>\n",
       "      <td>3349.0</td>\n",
       "      <td>3349.0</td>\n",
       "      <td>3350.0</td>\n",
       "      <td>3346.0</td>\n",
       "      <td>17556</td>\n",
       "      <td>5.877604e+08</td>\n",
       "      <td>2839668</td>\n",
       "      <td>0.001196</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    open   close    high     low  volume         money  \\\n",
       "date                                                                     \n",
       "2019-09-02 09:01  3350.0  3342.0  3350.0  3334.0   45292  1.512974e+09   \n",
       "2019-09-02 09:02  3341.0  3343.0  3347.0  3341.0   33174  1.109131e+09   \n",
       "2019-09-02 09:03  3343.0  3345.0  3346.0  3340.0   15492  5.178647e+08   \n",
       "2019-09-02 09:04  3345.0  3348.0  3352.0  3345.0   25934  8.686266e+08   \n",
       "2019-09-02 09:05  3349.0  3349.0  3350.0  3346.0   17556  5.877604e+08   \n",
       "\n",
       "                  open_interest  ret_rate  \n",
       "date                                       \n",
       "2019-09-02 09:01        2842292       NaN  \n",
       "2019-09-02 09:02        2836054       NaN  \n",
       "2019-09-02 09:03        2834260  0.000898  \n",
       "2019-09-02 09:04        2838874  0.001496  \n",
       "2019-09-02 09:05        2839668  0.001196  "
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_nan = all_data[\"ret_rate\"][:2].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.nan == test_nan[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.isnan(test_nan[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0031046992581667196"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_data[[\"high\", \"ret_rate\"]].corr().values[0, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from deap import gp, creator, algorithms, tools\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "pset = gp.PrimitiveSet?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "pset = gp.PrimitiveSet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "pset.addPrimitive?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "import operator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "pset = gp.PrimitiveSet(\"Main\", 2)\n",
    "pset.addPrimitive(max, 2)\n",
    "pset.addPrimitive(operator.add, 2)\n",
    "pset.addPrimitive(operator.mul, 2)\n",
    "pset.addTerminal(3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "# gp.PrimitiveSet(name, arity, prefix='ARG')\n",
    "gp.PrimitiveSet?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "gp.Primitive?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "pr = gp.Primitive(\"mul\", (int, int), int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'mul(1, 2)'"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pr.format(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pset.addPrimitive(primitive, arity, name=None)\n",
    "pset.addPrimitive?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ARG0', 'ARG1']"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pset.arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "pset.renameArguments(ARG0=\"x\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['x', 'ARG1']"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pset.arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "pset.renameArguments(ARG1=\"y\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['x', 'y']"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pset.arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "pset.ret?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "pset.addPrimitive(operator.neg, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "expr = gp.genFull(pset, min_=1, max_=3)\n",
    "tree = gp.PrimitiveTree(expr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'neg(3)'"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(tree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "gp.PrimitiveSetTyped?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "def if_then_else(input_, output1, output2):\n",
    "    \n",
    "    return output1 if input_ else output2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "pset = gp.PrimitiveSetTyped(\"main\", [bool, float], float)\n",
    "pset.addPrimitive(operator.xor, [bool, bool], bool)\n",
    "pset.addPrimitive(operator.mul, [float, float], float)\n",
    "pset.addPrimitive(if_then_else, [bool, float, float], float)\n",
    "pset.addTerminal(3.0, float)\n",
    "pset.addTerminal(1, bool)\n",
    "\n",
    "pset.renameArguments(ARG0=\"x\")\n",
    "pset.renameArguments(ARG1=\"y\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['x', 'y']"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pset.arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "pset.addEphemeralConstant(\"ephemeral cons2\", lambda: random.uniform(-1, 1), int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = lambda: random.randint(-10, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmp()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "pset.addEphemeralConstant"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [],
   "source": [
    "creator.create?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [],
   "source": [
    "from deap import base, tools, gp, algorithms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [],
   "source": [
    "creator.create(\"FitnessMin\", base.Fitness, weights=(-1.0,))\n",
    "creator.create(\"Individual\", gp.PrimitiveTree, fitness=creator.FitnessMin)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [],
   "source": [
    "toolbox = base.Toolbox()\n",
    "toolbox.register(\"expr\", gp.genFull, min_=1, max_=3, pset=pset)\n",
    "toolbox.register(\"individual\", tools.initIterate, creator.Individual, toolbox.expr)\n",
    "toolbox.register(\"population\", tools.initRepeat, list, toolbox.individual)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "add(1, -1)\n"
     ]
    }
   ],
   "source": [
    "ind = toolbox.individual()\n",
    "print(str(ind))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "pop = toolbox.population(n=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mul(0, 0)\n",
      "add(0, ARG0)\n",
      "mul(-1, -1)\n",
      "add(mul(ARG0, ARG0), add(1, 1))\n",
      "neg(sub(neg(ARG0), cos(1)))\n",
      "sin(0)\n",
      "cos(sub(ARG0, 0))\n",
      "mul(sub(sin(ARG0), cos(ARG0)), sub(mul(0, 1), sub(ARG0, ARG0)))\n",
      "cos(ARG0)\n",
      "neg(ARG0)\n"
     ]
    }
   ],
   "source": [
    "for expr in pop:\n",
    "    print(str(expr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'if_then_else(xor(x, 1), if_then_else(x, y, 3.0), mul(3.0, 3.0))'"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(toolbox.individual())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mul(if_then_else(xor(xor(xor(x, 1), xor(x, x)), xor(xor(1, x), xor(x, 1))), if_then_else(xor(xor(1, x), xor(1, 1)), if_then_else(xor(1, 1), mul(3.0, y), mul(y, 3.0)), mul(if_then_else(1, y, y), if_then_else(x, y, y))), mul(if_then_else(xor(1, 1), mul(3.0, y), mul(y, y)), if_then_else(xor(1, x), mul(y, y), mul(y, 3.0)))), if_then_else(xor(xor(xor(1, x), xor(1, 1)), xor(xor(x, x), xor(x, 1))), mul(if_then_else(xor(x, 1), if_then_else(1, y, y), mul(3.0, y)), if_then_else(xor(1, x), if_then_else(x, 3.0, 3.0), mul(3.0, y))), if_then_else(xor(xor(1, 1), xor(x, x)), mul(mul(y, 3.0), mul(3.0, 3.0)), if_then_else(xor(1, 1), if_then_else(x, 3.0, 3.0), if_then_else(1, 3.0, 3.0)))))\n"
     ]
    }
   ],
   "source": [
    "expr = gp.genFull(pset, min_=1, max_=7)\n",
    "tree = gp.PrimitiveTree(expr)\n",
    "print(str(tree))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [],
   "source": [
    "gp.compile?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [],
   "source": [
    "func = gp.compile(tree, pset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function <lambda>(x, y)>"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "func"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "72.0"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "func(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'mul(if_then_else(xor(xor(xor(x, 1), xor(x, x)), xor(xor(1, x), xor(x, 1))), if_then_else(xor(xor(1, x), xor(1, 1)), if_then_else(xor(1, 1), mul(3.0, y), mul(y, 3.0)), mul(if_then_else(1, y, y), if_then_else(x, y, y))), mul(if_then_else(xor(1, 1), mul(3.0, y), mul(y, y)), if_then_else(xor(1, x), mul(y, y), mul(y, 3.0)))), if_then_else(xor(xor(xor(1, x), xor(1, 1)), xor(xor(x, x), xor(x, 1))), mul(if_then_else(xor(x, 1), if_then_else(1, y, y), mul(3.0, y)), if_then_else(xor(1, x), if_then_else(x, 3.0, 3.0), mul(3.0, y))), if_then_else(xor(xor(1, 1), xor(x, x)), mul(mul(y, 3.0), mul(3.0, 3.0)), if_then_else(xor(1, 1), if_then_else(x, 3.0, 3.0), if_then_else(1, 3.0, 3.0)))))'"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(tree)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Symbolic Regression Problem: Introduction to GP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [],
   "source": [
    "import operator\n",
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pset.renameArguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "metadata": {},
   "outputs": [],
   "source": [
    "def protectedDiv(left, right):\n",
    "    try:\n",
    "        return left / right\n",
    "    except ZeroDivisionError:\n",
    "        return 1\n",
    "    \n",
    "pset = gp.PrimitiveSet(\"Main\", arity=1)\n",
    "pset.renameArguments(ARG0=\"x\")\n",
    "pset.addPrimitive(operator.add, 2)\n",
    "pset.addPrimitive(operator.sub, 2)\n",
    "pset.addPrimitive(operator.mul, 2)\n",
    "pset.addPrimitive(protectedDiv, 2)\n",
    "pset.addPrimitive(operator.neg, 1)\n",
    "pset.addPrimitive(math.cos, 1)\n",
    "pset.addPrimitive(math.sin, 1)\n",
    "pset.addEphemeralConstant(\"rand\", lambda: random.randint(-1, 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {},
   "outputs": [],
   "source": [
    "toolbox = base.Toolbox()\n",
    "toolbox.register(\"expr\", gp.genHalfAndHalf, pset=pset, min_=1, max_=3)\n",
    "toolbox.register(\"individual\", tools.initIterate, creator.Individual, toolbox.expr)\n",
    "toolbox.register(\"population\", tools.initRepeat, list, toolbox.individual)\n",
    "toolbox.register(\"compile\", gp.compile, pset=pset)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "metadata": {},
   "outputs": [],
   "source": [
    "def evalSymReg(individual, points):\n",
    "    \n",
    "    func = toolbox.compile(expr=individual)\n",
    "    mse = ((func(x) - x**4 - x**3 - x**2 - x)**2 for x in points)\n",
    "    return (math.fsum(mse) / len(points),)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "metadata": {},
   "outputs": [],
   "source": [
    "toolbox.register(\"evaluate\", evalSymReg, points=[x/10. for x in range(-10, 10)])\n",
    "toolbox.register(\"select\", tools.selTournament, tournsize=3)\n",
    "toolbox.register(\"mate\", gp.cxOnePoint)  # tools.cxOnePoint不行\n",
    "toolbox.register(\"Expr_mut\", gp.genFull, min_=0, max_=3)\n",
    "toolbox.register(\"mutate\", gp.mutUniform, expr=toolbox.Expr_mut, pset=pset)\n",
    "\n",
    "toolbox.decorate(\"mate\", gp.staticLimit(key=operator.attrgetter(\"height\"), max_value=17))\n",
    "toolbox.decorate(\"mutate\", gp.staticLimit(key=operator.attrgetter(\"height\"), max_value=17))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 202,
   "metadata": {},
   "outputs": [],
   "source": [
    "stats_fit = tools.Statistics(lambda ind: ind.fitness.values)\n",
    "stats_size = tools.Statistics(len)\n",
    "mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size)\n",
    "mstats.register(\"avg\", np.mean)\n",
    "mstats.register(\"std\", np.std)\n",
    "mstats.register(\"min\", np.min)\n",
    "mstats.register(\"max\", np.max)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   \t      \t                        fitness                        \t                      size                     \n",
      "   \t      \t-------------------------------------------------------\t-----------------------------------------------\n",
      "gen\tnevals\tavg    \tgen\tmax    \tmin     \tnevals\tstd    \tavg    \tgen\tmax\tmin\tnevals\tstd    \n",
      "0  \t30    \t37.6598\t0  \t1054.64\t0.564583\t30    \t188.888\t5.16667\t0  \t13 \t2  \t30    \t3.08851\n",
      "1  \t8     \t0.957479\t1  \t2.3179 \t0.328818\t8     \t0.437123\t4.36667\t1  \t9  \t2  \t8     \t2.10528\n",
      "2  \t19    \t1.12016 \t2  \t9.12456\t0.328818\t19    \t1.59192 \t4.06667\t2  \t10 \t2  \t19    \t2.0155 \n",
      "3  \t22    \t0.942414\t3  \t4.30874\t0.328818\t22    \t0.944506\t4.7    \t3  \t18 \t3  \t22    \t3.06757\n",
      "4  \t13    \t0.764611\t4  \t2.552  \t0.328818\t13    \t0.62188 \t5.03333\t4  \t15 \t2  \t13    \t2.93806\n",
      "5  \t9     \t0.644352\t5  \t2.54124\t0.328818\t9     \t0.571552\t4.9    \t5  \t10 \t1  \t9     \t2.15019\n",
      "6  \t9     \t0.447934\t6  \t1.43276\t0.328818\t9     \t0.273102\t4.6    \t6  \t18 \t3  \t9     \t2.67831\n",
      "7  \t14    \t0.719733\t7  \t2.9829 \t0.328818\t14    \t0.641574\t4.66667\t7  \t15 \t1  \t14    \t2.59915\n",
      "8  \t17    \t0.648241\t8  \t2.12845\t0.328818\t17    \t0.429727\t5.06667\t8  \t14 \t3  \t17    \t2.70719\n",
      "9  \t23    \t0.726997\t9  \t2.54124\t0.328818\t23    \t0.586643\t4.96667\t9  \t13 \t3  \t23    \t2.50976\n",
      "10 \t14    \t0.596716\t10 \t2.12845\t0.328818\t14    \t0.421487\t5.63333\t10 \t13 \t3  \t14    \t3.63761\n",
      "11 \t18    \t0.823742\t11 \t4.35469\t0.305641\t18    \t0.926616\t4.9    \t11 \t13 \t3  \t18    \t2.34307\n",
      "12 \t15    \t0.578251\t12 \t2.12845\t0.305641\t15    \t0.446226\t6.4    \t12 \t16 \t3  \t15    \t3.67514\n",
      "13 \t19    \t1.02206 \t13 \t16.4604\t0.305641\t19    \t2.89384 \t7.56667\t13 \t16 \t3  \t19    \t4.04708\n",
      "14 \t18    \t0.638947\t14 \t4.79431\t0.305641\t18    \t0.848334\t7.46667\t14 \t15 \t3  \t18    \t3.64905\n",
      "15 \t19    \t0.404035\t15 \t1.31766\t0.305641\t19    \t0.214555\t9.1    \t15 \t20 \t3  \t19    \t4.22177\n",
      "16 \t20    \t0.47012 \t16 \t1.16579\t0.305641\t20    \t0.235686\t11.4667\t16 \t19 \t4  \t20    \t3.97268\n",
      "17 \t21    \t0.665546\t17 \t4.48395\t0.291809\t21    \t0.828406\t13.6333\t17 \t32 \t3  \t21    \t7.55638\n",
      "18 \t19    \t0.462164\t18 \t1.52449\t0.291809\t19    \t0.280935\t13.3667\t18 \t27 \t3  \t19    \t5.99713\n",
      "19 \t18    \t0.535172\t19 \t2.54124\t0.291809\t18    \t0.471767\t14.0333\t19 \t22 \t3  \t18    \t5.3134 \n",
      "20 \t11    \t0.325216\t20 \t0.695352\t0.291809\t11    \t0.0719402\t15.0333\t20 \t27 \t4  \t11    \t5.90188\n",
      "21 \t15    \t0.36558 \t21 \t0.75108 \t0.291809\t15    \t0.116067 \t15.2667\t21 \t26 \t3  \t15    \t6.98538\n",
      "22 \t16    \t0.451421\t22 \t1.11355 \t0.193912\t16    \t0.234722 \t13.8333\t22 \t35 \t4  \t16    \t7.42556\n",
      "23 \t11    \t0.638231\t23 \t4.87023 \t0.193912\t11    \t1.0537   \t15     \t23 \t42 \t4  \t11    \t9.02589\n",
      "24 \t23    \t36.8007 \t24 \t1085.25 \t0.173221\t23    \t194.694  \t11.4667\t24 \t27 \t3  \t23    \t6.26489\n",
      "25 \t19    \t0.543945\t25 \t2.76958 \t0.165572\t19    \t0.649589 \t10.0667\t25 \t19 \t5  \t19    \t3.96597\n",
      "26 \t20    \t0.348469\t26 \t1.45715 \t0.142554\t20    \t0.316604 \t9.66667\t26 \t19 \t3  \t20    \t3.73571\n",
      "27 \t18    \t0.47028 \t27 \t1.7196  \t0.0976781\t18    \t0.442292 \t9.5    \t27 \t16 \t3  \t18    \t3.34415\n",
      "28 \t18    \t0.374259\t28 \t1.63803 \t0.0173316\t18    \t0.443341 \t9.9    \t28 \t17 \t3  \t18    \t2.98161\n",
      "29 \t18    \t0.250633\t29 \t1.74671 \t0.0173316\t18    \t0.345059 \t10.9333\t29 \t23 \t3  \t18    \t3.67816\n",
      "30 \t22    \t0.273681\t30 \t1.6835  \t0.0173316\t22    \t0.373205 \t12.7333\t30 \t24 \t5  \t22    \t4.37366\n",
      "31 \t21    \t0.256248\t31 \t1.63803 \t0.0173316\t21    \t0.36811  \t12.2667\t31 \t28 \t2  \t21    \t5.35993\n",
      "32 \t14    \t0.114154\t32 \t0.777593\t0.0173316\t14    \t0.152647 \t13.0667\t32 \t27 \t5  \t14    \t4.58936\n",
      "33 \t21    \t0.212907\t33 \t0.777593\t0.0173316\t21    \t0.209776 \t16.2333\t33 \t28 \t7  \t21    \t5.10348\n",
      "34 \t10    \t0.0821662\t34 \t0.365572\t8.82076e-33\t10    \t0.0852193\t17.1   \t34 \t34 \t7  \t10    \t5.46107\n",
      "35 \t20    \t0.295333 \t35 \t2.54124 \t0.0173316  \t20    \t0.552286 \t17.5667\t35 \t31 \t3  \t20    \t5.7659 \n",
      "36 \t17    \t0.264726 \t36 \t2.60979 \t8.82076e-33\t17    \t0.614509 \t16.5333\t36 \t29 \t7  \t17    \t4.57335\n",
      "37 \t13    \t0.206646 \t37 \t1.96185 \t0.0049529  \t13    \t0.408924 \t16.5   \t37 \t32 \t3  \t13    \t6.88356\n",
      "38 \t19    \t0.114107 \t38 \t0.624819\t0.0049529  \t19    \t0.1494   \t16.5   \t38 \t34 \t7  \t19    \t5.52419\n",
      "39 \t20    \t0.171625 \t39 \t0.590434\t0.0049529  \t20    \t0.174614 \t16.6   \t39 \t27 \t7  \t20    \t4.11987\n",
      "40 \t27    \t0.165071 \t40 \t1.10624 \t0.0173316  \t27    \t0.231605 \t14.6667\t40 \t21 \t6  \t27    \t3.72678\n"
     ]
    }
   ],
   "source": [
    "pop = toolbox.population(n=30)\n",
    "hof = tools.HallOfFame(1)\n",
    "pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, 40, stats=mstats, halloffame=hof, verbose=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'add(add(mul(add(mul(x, x), mul(x, mul(x, x))), x), mul(x, x)), x)'"
      ]
     },
     "execution_count": 204,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(hof[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from deap import creator, gp, tools, algorithms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "def lf(x): return 1 / (1 + math.exp(-x));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "import operator\n",
    "\n",
    "pset = gp.PrimitiveSet(\"Main\", 2)\n",
    "pset.addPrimitive(operator.sub, 2)\n",
    "# pset.addPrimitive(If, 1, name=\"If\")\n",
    "pset.addPrimitive(lf, 1, name=\"lf\")\n",
    "pset.addTerminal(3)\n",
    "pset.addPrimitive(operator.add, 2)\n",
    "pset.addPrimitive(operator.mul, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "ind1 = gp.genGrow(pset, 1, 3)\n",
    "ind2 = gp.genGrow(pset, 1, 3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<deap.gp.Primitive at 0x7fa36d8b4548>,\n",
       " <deap.gp.Terminal at 0x7fa36d9aedc8>,\n",
       " <deap.gp.Primitive at 0x7fa36d8b4598>,\n",
       " <deap.gp.Primitive at 0x7fa36d619ae8>,\n",
       " <deap.gp.Terminal at 0x7fa36d9aedc8>,\n",
       " <deap.gp.Primitive at 0x7fa36d619b38>,\n",
       " <deap.gp.Terminal at 0x7fa36d9aedc8>,\n",
       " <deap.gp.Terminal at 0x7fa36d9aedc8>]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ind1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<deap.gp.Primitive at 0x7fa36d619ae8>, <deap.gp.Terminal at 0x7fa36d9aedc8>]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ind2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'add(3, mul(lf(3), sub(3, 3)))'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tree = gp.PrimitiveTree(ind1)\n",
    "str(tree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "new_ind1, new_ind2 = gp.cxSemantic(ind1, ind2, pset=pset, max=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'add(mul(add(3, mul(lf(3), sub(3, 3))), lf(add(lf(ARG1), add(ARG1, ARG0)))), mul(sub(1.0, lf(add(lf(ARG1), add(ARG1, ARG0)))), lf(3)))'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tree_new_ind1 = gp.PrimitiveTree(new_ind1)\n",
    "str(tree_new_ind1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'add'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ind1[0].name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140339895103560\n",
      "140339895103560\n"
     ]
    }
   ],
   "source": [
    "\n",
    "class test:\n",
    "    def __init__(self):\n",
    "        self.a = [1, 2, 3, 4]\n",
    "        print(id(self.a))\n",
    "    def func2(self):\n",
    "        tmp = self.a\n",
    "        print(id(tmp))\n",
    "t = test()\n",
    "t.func2()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140339895003656\n",
      "140339895105480\n"
     ]
    }
   ],
   "source": [
    "test_ls = list(range(10))\n",
    "print(id(test_ls))\n",
    "test_ls2 = [x for x in test_ls if x > 5]\n",
    "print(id(test_ls2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ls_1 140339895115976\n",
      "ls1 140339895132744\n",
      "ls2 140339895133064\n",
      "ls3 140339895133192\n",
      "140339895133064 ==== 140339895133192\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]\n"
     ]
    }
   ],
   "source": [
    "ls_1 = list()\n",
    "print(\"ls_1\", id(ls_1))\n",
    "ls1 = list(range(10))\n",
    "print(\"ls1\", id(ls1))\n",
    "ls2 = list(range(20))\n",
    "print(\"ls2\", id(ls2))\n",
    "ls3 = list(range(30))\n",
    "print(\"ls3\", id(ls3))\n",
    "ls_1.append(ls1)\n",
    "ls_1.append(ls2)\n",
    "ls_1.append(ls3)\n",
    "\n",
    "temp = [each for each in ls_1 if len(each) > 15]\n",
    "temp2 = temp\n",
    "temp2[0].pop(-1)\n",
    "print(id(temp[0]), \"====\", id(temp[1]))\n",
    "print(ls_1[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = {\"a\":10, \"b\":20}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 10, 'b': 20}"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.pop?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"a\" in a.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(str(time.strftime(\"%Y%m%d%H%M%S\", time.localtime())) +\n",
    "          \"checkpoint_name.pkl\", \"wb\") as cp_file:\n",
    "    cp = pickle.load(cp_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "180000"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "360 * 250*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 3]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[1, 2, 3] + [3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "test = np.array([range(10), [1]*10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1\n",
       "0  0  1\n",
       "1  1  1\n",
       "2  2  1\n",
       "3  3  1\n",
       "4  4  1\n",
       "5  5  1\n",
       "6  6  1\n",
       "7  7  1\n",
       "8  8  1\n",
       "9  9  1"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1., nan],\n",
       "       [nan, nan]])"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.corr(\"pearson\").values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"RB8888_1m_20190901_20210901.csv\") as file:\n",
    "    raw_data = pd.read_csv(file, sep=\",\", index_col=\"date\", na_values=['#VALUE!', '#DIV/0!'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9999904062831156"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_data[\"open\"].corr(raw_data[\"close\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2, 3]\n",
    "a[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'202109291532'"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.strftime(\"%Y%m%d%H%M\", time.localtime())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "with open(\"ret_factor.csv\") as file:\n",
    "    raw_data = pd.read_csv(file, sep=\",\", index_col=\"date\", na_values=['#VALUE!', '#DIV/0!'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ret_rate</th>\n",
       "      <th>factor_value</th>\n",
       "      <th>gp_min(gp_div(gp_sub(5, var1), gp_add(var0, 6)), gp_sub(gp_sub(var0, var0), gp_min(var1, var2)))</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:03</th>\n",
       "      <td>1.225735</td>\n",
       "      <td>-2.143547</td>\n",
       "      <td>-2.143547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:04</th>\n",
       "      <td>1.988789</td>\n",
       "      <td>-2.143547</td>\n",
       "      <td>-2.143547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:05</th>\n",
       "      <td>1.712564</td>\n",
       "      <td>-2.143547</td>\n",
       "      <td>-2.143547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:06</th>\n",
       "      <td>2.178708</td>\n",
       "      <td>-2.143547</td>\n",
       "      <td>-2.143547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-02 09:07</th>\n",
       "      <td>1.172691</td>\n",
       "      <td>-2.143547</td>\n",
       "      <td>-2.143547</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  ret_rate  factor_value  \\\n",
       "date                                       \n",
       "2019-09-02 09:03  1.225735     -2.143547   \n",
       "2019-09-02 09:04  1.988789     -2.143547   \n",
       "2019-09-02 09:05  1.712564     -2.143547   \n",
       "2019-09-02 09:06  2.178708     -2.143547   \n",
       "2019-09-02 09:07  1.172691     -2.143547   \n",
       "\n",
       "                  gp_min(gp_div(gp_sub(5, var1), gp_add(var0, 6)), gp_sub(gp_sub(var0, var0), gp_min(var1, var2)))  \n",
       "date                                                                                                                \n",
       "2019-09-02 09:03                                          -2.143547                                                 \n",
       "2019-09-02 09:04                                          -2.143547                                                 \n",
       "2019-09-02 09:05                                          -2.143547                                                 \n",
       "2019-09-02 09:06                                          -2.143547                                                 \n",
       "2019-09-02 09:07                                          -2.143547                                                 "
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_data.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ret_rate</th>\n",
       "      <th>factor_value</th>\n",
       "      <th>gp_min(gp_div(gp_sub(5, var1), gp_add(var0, 6)), gp_sub(gp_sub(var0, var0), gp_min(var1, var2)))</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-09-01 14:56</th>\n",
       "      <td>1.946305</td>\n",
       "      <td>-0.056924</td>\n",
       "      <td>-0.056924</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-01 14:57</th>\n",
       "      <td>-0.005506</td>\n",
       "      <td>-0.352652</td>\n",
       "      <td>-0.352652</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-01 14:58</th>\n",
       "      <td>-1.459727</td>\n",
       "      <td>-0.352646</td>\n",
       "      <td>-0.352646</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-01 14:59</th>\n",
       "      <td>-2.520445</td>\n",
       "      <td>-0.285773</td>\n",
       "      <td>-0.285773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-01 15:00</th>\n",
       "      <td>-2.493217</td>\n",
       "      <td>-0.285767</td>\n",
       "      <td>-0.285767</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  ret_rate  factor_value  \\\n",
       "date                                       \n",
       "2021-09-01 14:56  1.946305     -0.056924   \n",
       "2021-09-01 14:57 -0.005506     -0.352652   \n",
       "2021-09-01 14:58 -1.459727     -0.352646   \n",
       "2021-09-01 14:59 -2.520445     -0.285773   \n",
       "2021-09-01 15:00 -2.493217     -0.285767   \n",
       "\n",
       "                  gp_min(gp_div(gp_sub(5, var1), gp_add(var0, 6)), gp_sub(gp_sub(var0, var0), gp_min(var1, var2)))  \n",
       "date                                                                                                                \n",
       "2021-09-01 14:56                                          -0.056924                                                 \n",
       "2021-09-01 14:57                                          -0.352652                                                 \n",
       "2021-09-01 14:58                                          -0.352646                                                 \n",
       "2021-09-01 14:59                                          -0.285773                                                 \n",
       "2021-09-01 15:00                                          -0.285767                                                 "
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_data.tail(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.0015979100863265743"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_data[\"ret_rate\"].corr(raw_data[\"factor_value\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data.to_csv?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "def func(var0, var1, var2):\n",
    "    return ((5-var1) / (var0+6), (var0-var0) - min(var1, var2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date\n",
       "2019-09-02 09:03   -2.143547\n",
       "2019-09-02 09:04   -2.143547\n",
       "2019-09-02 09:05   -2.143547\n",
       "2019-09-02 09:06   -2.143547\n",
       "2019-09-02 09:07   -2.143547\n",
       "2019-09-02 09:08   -2.143547\n",
       "2019-09-02 09:09   -2.143547\n",
       "2019-09-02 09:10   -2.143547\n",
       "2019-09-02 09:11   -0.573762\n",
       "2019-09-02 09:12   -0.573755\n",
       "Name: factor_value, dtype: float64"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_data.iloc[:, 1][:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data = raw_data.astype(np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date\n",
       "2019-09-02 09:03   -2.143547\n",
       "2019-09-02 09:04   -2.143547\n",
       "2019-09-02 09:05   -2.143547\n",
       "2019-09-02 09:06   -2.143547\n",
       "2019-09-02 09:07   -2.143547\n",
       "2019-09-02 09:08   -2.143547\n",
       "2019-09-02 09:09   -2.143547\n",
       "2019-09-02 09:10   -2.143547\n",
       "2019-09-02 09:11   -0.573762\n",
       "2019-09-02 09:12   -0.573755\n",
       "Name: factor_value, dtype: float32"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_data.iloc[:, 1][:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>volume/volume_lag1</th>\n",
       "      <th>volume/volume_lag2</th>\n",
       "      <th>volume_lag1/volume_lag2</th>\n",
       "      <th>ret_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-09-02 09:03</td>\n",
       "      <td>0.521915</td>\n",
       "      <td>0.349809</td>\n",
       "      <td>0.670241</td>\n",
       "      <td>0.001043</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-09-02 09:04</td>\n",
       "      <td>1.746168</td>\n",
       "      <td>0.911352</td>\n",
       "      <td>0.521915</td>\n",
       "      <td>0.001688</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-09-02 09:05</td>\n",
       "      <td>0.711134</td>\n",
       "      <td>1.241759</td>\n",
       "      <td>1.746168</td>\n",
       "      <td>0.001454</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-09-02 09:06</td>\n",
       "      <td>1.229436</td>\n",
       "      <td>0.874294</td>\n",
       "      <td>0.711134</td>\n",
       "      <td>0.001848</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-09-02 09:07</td>\n",
       "      <td>0.756859</td>\n",
       "      <td>0.930510</td>\n",
       "      <td>1.229436</td>\n",
       "      <td>0.000999</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               date  volume/volume_lag1  volume/volume_lag2  \\\n",
       "0  2019-09-02 09:03            0.521915            0.349809   \n",
       "1  2019-09-02 09:04            1.746168            0.911352   \n",
       "2  2019-09-02 09:05            0.711134            1.241759   \n",
       "3  2019-09-02 09:06            1.229436            0.874294   \n",
       "4  2019-09-02 09:07            0.756859            0.930510   \n",
       "\n",
       "   volume_lag1/volume_lag2  ret_rate  \n",
       "0                 0.670241  0.001043  \n",
       "1                 0.521915  0.001688  \n",
       "2                 1.746168  0.001454  \n",
       "3                 0.711134  0.001848  \n",
       "4                 1.229436  0.000999  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(\"cal_factor_data.csv\")\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "func=gp_sub(gp_sub(gp_mul(var2, var0), gp_max(1, var1)), var0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data = data.copy()\n",
    "low_val = test_data[\"volume/volume_lag1\"][test_data[\"volume/volume_lag1\"] < 1].copy()\n",
    "high_val = test_data[\"volume/volume_lag1\"][test_data[\"volume/volume_lag1\"] >= 1].copy()\n",
    "low_val_be1 = pd.Series([1]*len(low_val), dtype=np.float64, index=low_val.index)\n",
    "high_val = high_val.append(low_val_be1)\n",
    "high_val.sort_index(inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data[\"factor\"] = high_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "158921"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(high_val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>volume/volume_lag1</th>\n",
       "      <th>volume/volume_lag2</th>\n",
       "      <th>volume_lag1/volume_lag2</th>\n",
       "      <th>ret_rate</th>\n",
       "      <th>factor</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-09-02 09:03</td>\n",
       "      <td>0.521915</td>\n",
       "      <td>0.349809</td>\n",
       "      <td>0.670241</td>\n",
       "      <td>0.001043</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-09-02 09:04</td>\n",
       "      <td>1.746168</td>\n",
       "      <td>0.911352</td>\n",
       "      <td>0.521915</td>\n",
       "      <td>0.001688</td>\n",
       "      <td>1.746168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-09-02 09:05</td>\n",
       "      <td>0.711134</td>\n",
       "      <td>1.241759</td>\n",
       "      <td>1.746168</td>\n",
       "      <td>0.001454</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-09-02 09:06</td>\n",
       "      <td>1.229436</td>\n",
       "      <td>0.874294</td>\n",
       "      <td>0.711134</td>\n",
       "      <td>0.001848</td>\n",
       "      <td>1.229436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-09-02 09:07</td>\n",
       "      <td>0.756859</td>\n",
       "      <td>0.930510</td>\n",
       "      <td>1.229436</td>\n",
       "      <td>0.000999</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               date  volume/volume_lag1  volume/volume_lag2  \\\n",
       "0  2019-09-02 09:03            0.521915            0.349809   \n",
       "1  2019-09-02 09:04            1.746168            0.911352   \n",
       "2  2019-09-02 09:05            0.711134            1.241759   \n",
       "3  2019-09-02 09:06            1.229436            0.874294   \n",
       "4  2019-09-02 09:07            0.756859            0.930510   \n",
       "\n",
       "   volume_lag1/volume_lag2  ret_rate    factor  \n",
       "0                 0.670241  0.001043  1.000000  \n",
       "1                 0.521915  0.001688  1.746168  \n",
       "2                 1.746168  0.001454  1.000000  \n",
       "3                 0.711134  0.001848  1.229436  \n",
       "4                 1.229436  0.000999  1.000000  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data[\"factor\"] = test_data[\"volume_lag1/volume_lag2\"] * test_data[\"volume/volume_lag1\"] - test_data[\"factor\"] - test_data[\"volume/volume_lag1\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "gp_min(gp_div(gp_sub(5, var1), gp_add(var0, 6)), gp_sub(gp_sub(var0, var0), gp_min(var1, var2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    1.589210e+05\n",
       "mean              inf\n",
       "std               NaN\n",
       "min     -3.033167e+02\n",
       "25%     -1.601163e+00\n",
       "50%     -1.131084e+00\n",
       "75%     -7.448404e-01\n",
       "max               inf\n",
       "Name: factor, dtype: float64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data[\"factor\"].describe()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data.replace([np.inf, -np.inf], np.nan, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    158919.000000\n",
       "mean         -1.294396\n",
       "std           2.032514\n",
       "min        -303.316680\n",
       "25%          -1.601184\n",
       "50%          -1.131130\n",
       "75%          -0.744845\n",
       "max         261.086746\n",
       "Name: factor, dtype: float64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data[\"factor\"].describe()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "factor = pd.DataFrame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.5219154065307912,\n",
       " 1.7461683812723074,\n",
       " 0.7111338222916918,\n",
       " 1.2294361315411275,\n",
       " 0.7568589699511793,\n",
       " 0.7012810029980922,\n",
       " 1.2615623785464438,\n",
       " 0.4277058944341754,\n",
       " 1.5913565426170468,\n",
       " 0.9087205793602896,\n",
       " 1.0999501909347502,\n",
       " 0.6072452830188679,\n",
       " 0.9751429281630624,\n",
       " 1.3734386948763702,\n",
       " 0.8888270230141054,\n",
       " 2.6855293380664023,\n",
       " 0.6258455796594355,\n",
       " 0.55721207603429,\n",
       " 1.109253065774805,\n",
       " 0.5610050251256281,\n",
       " 1.0856323898244358,\n",
       " 1.1970297029702972,\n",
       " 0.947339398952302,\n",
       " 1.2136204889406286,\n",
       " 0.6,\n",
       " 1.140287769784173,\n",
       " 1.003154574132492,\n",
       " 1.705800139762404,\n",
       " 0.9207292093404342,\n",
       " 1.5439377085650725,\n",
       " 1.3093659942363112,\n",
       " 0.6698580389567514,\n",
       " 0.9748644652538196,\n",
       " 0.9764071452645772,\n",
       " 1.2298929927511222,\n",
       " 1.203480213303396,\n",
       " 0.6107742537313433,\n",
       " 0.8795341733486063,\n",
       " 1.1104840460169307,\n",
       " 1.5279515246286162,\n",
       " 1.2005884610464372,\n",
       " 0.4494405966968567,\n",
       " 1.3385490753911806,\n",
       " 0.5859015232022671,\n",
       " 0.5392986698911729,\n",
       " 1.6956278026905829,\n",
       " 0.7619834710743801,\n",
       " 0.6221258134490238,\n",
       " 2.8375174337517435,\n",
       " 0.6913246497911034,\n",
       " 1.5574120156416638,\n",
       " 3.1065966674275285,\n",
       " 0.677443056576047,\n",
       " 0.8506507592190892,\n",
       " 1.3895193165880404,\n",
       " 1.0595522114149385,\n",
       " 0.3771542391963281,\n",
       " 1.2631458094144659,\n",
       " 1.1041628794764589,\n",
       " 0.9935791899901218,\n",
       " 0.5325600662800332,\n",
       " 0.8609209707529558,\n",
       " 0.6035417419588002,\n",
       " 0.9473053892215568,\n",
       " 2.0189633375474085,\n",
       " 2.4082654978083906,\n",
       " 0.8373634945397815,\n",
       " 0.4420121099208197,\n",
       " 0.7348085704250088,\n",
       " 1.4832695984703632,\n",
       " 0.9203996132774734,\n",
       " 0.4611344537815126,\n",
       " 1.231586940015186,\n",
       " 7.588779284833539,\n",
       " 1.4831424161182871,\n",
       " 0.3029141104294479,\n",
       " 1.6790235081374325,\n",
       " 1.236295099623048,\n",
       " 1.3631849464239043,\n",
       " 1.3914238241308794,\n",
       " 0.5279933863041382,\n",
       " 1.9452853166318718,\n",
       " 0.5969234896927962,\n",
       " 0.6261892276575024,\n",
       " 1.3621246560593372,\n",
       " 0.5551554540663973,\n",
       " 1.4461319411485525,\n",
       " 0.9175144951318236,\n",
       " 0.5923452962918803,\n",
       " 0.6823671497584541,\n",
       " 1.6115044247787609,\n",
       " 3.047226798462383,\n",
       " 0.9353036583168138,\n",
       " 0.7459216441875401,\n",
       " 2.1267435853280525,\n",
       " 0.6010526315789474,\n",
       " 0.7324531860433787,\n",
       " 1.1947765311752805,\n",
       " 0.5268626847290641,\n",
       " 1.3929875821767714,\n",
       " 0.7216570529627687,\n",
       " 0.9324226129922976,\n",
       " 2.270729426433915,\n",
       " 0.5939323220536756,\n",
       " 0.557725644285219,\n",
       " 0.5261085785329466,\n",
       " 1.1039779440724695,\n",
       " 1.6268283981448448,\n",
       " 2.0407894736842107,\n",
       " 0.4820545884375672,\n",
       " 1.2231386535889437,\n",
       " 0.5644250045562238,\n",
       " 1.6038101388440429,\n",
       " 0.9148379303402456,\n",
       " 0.8182218309859155,\n",
       " 0.6976869284561592,\n",
       " 2.2039321511179644,\n",
       " 0.7169844323946125,\n",
       " 0.8182483532568919,\n",
       " 0.9862850327966608,\n",
       " 4.468863361547763,\n",
       " 0.4998308868294663,\n",
       " 0.8974150764650156,\n",
       " 2.0769114763987333,\n",
       " 0.9006680220737728,\n",
       " 0.5244276039987101,\n",
       " 2.1054573405073023,\n",
       " 0.4986127336448598,\n",
       " 0.6611509737882559,\n",
       " 0.9705426356589149,\n",
       " 0.6193518941122775,\n",
       " 1.5453205600589537,\n",
       " 2.3803051979017646,\n",
       " 1.540318541520585,\n",
       " 0.3472718995902972,\n",
       " 0.7833333333333333,\n",
       " 1.8577575902462349,\n",
       " 0.5771457984815339,\n",
       " 1.4916387959866217,\n",
       " 0.42077727952167415,\n",
       " 0.8863232682060391,\n",
       " 0.9795591182364728,\n",
       " 5.677577741407528,\n",
       " 0.4292303257422889,\n",
       " 1.035594358629953,\n",
       " 1.3916990920881969,\n",
       " 0.5168918918918919,\n",
       " 0.5490196078431373,\n",
       " 1.9417077175697863,\n",
       " 0.8737843551797041,\n",
       " 1.872731671909025,\n",
       " 0.5189922480620155,\n",
       " 0.7134677620114513,\n",
       " 2.8663642707606423,\n",
       " 0.3138161898965308,\n",
       " 1.2323506594259117,\n",
       " 1.483160214038401,\n",
       " 0.4881154499151104,\n",
       " 4.138260869565217,\n",
       " 1.3915738600546332,\n",
       " 0.8298225745564364,\n",
       " 2.05240651442089,\n",
       " 0.4860803262700594,\n",
       " 0.6785225718194254,\n",
       " 1.1271505376344086,\n",
       " 0.5357739088957787,\n",
       " 0.9187625194747384,\n",
       " 0.529796511627907,\n",
       " 1.3209876543209875,\n",
       " 1.3032191069574248,\n",
       " 0.4645418326693227,\n",
       " 3.5797598627787317,\n",
       " 0.15540648458712664,\n",
       " 2.36382322713258,\n",
       " 0.7752173913043479,\n",
       " 0.3634324172742569,\n",
       " 9.094135802469136,\n",
       " 0.8087561513660275,\n",
       " 0.38648762064624426,\n",
       " 0.9381107491856676,\n",
       " 1.6556712962962965,\n",
       " 1.8192939531632295,\n",
       " 2.8699327569644573,\n",
       " 0.8327085285848173,\n",
       " 0.5025323579065841,\n",
       " 1.1161414173732205,\n",
       " 0.8737279633080121,\n",
       " 0.7791994750656168,\n",
       " 2.671578947368421,\n",
       " 0.4189125295508274,\n",
       " 0.7834838224228743,\n",
       " 0.8729891956782713,\n",
       " 1.3512101210121013,\n",
       " 3.074292692855689,\n",
       " 0.4606064618644068,\n",
       " 0.3788989506971396,\n",
       " 5.198027314112292,\n",
       " 0.6668369581083053,\n",
       " 0.3855751340702638,\n",
       " 0.8952597218279875,\n",
       " 2.1613823715916296,\n",
       " 0.6496992812087429,\n",
       " 1.3644163468051478,\n",
       " 5.2801588614926365,\n",
       " 0.6714303622915884,\n",
       " 0.4469286781179985,\n",
       " 0.9676240208877284,\n",
       " 1.1848893685914732,\n",
       " 0.6134997267261796,\n",
       " 1.1132887899034891,\n",
       " 2.2752734062416646,\n",
       " 0.6811254396248535,\n",
       " 0.8800344234079174,\n",
       " 2.3397222765499706,\n",
       " 0.752236061188665,\n",
       " 0.8847094121569063,\n",
       " 0.5023550838409847,\n",
       " 0.9867483435429428,\n",
       " 0.9196756619789688,\n",
       " 1.9320843091334892,\n",
       " 0.6525490196078432,\n",
       " 1.3970716783216783,\n",
       " 1.6773033004849054,\n",
       " 1.1942087102489976,\n",
       " 0.6632696888055913,\n",
       " 1.0405604285630188,\n",
       " 0.9921928038017652,\n",
       " 0.5477819591743642,\n",
       " 0.7053190382013116,\n",
       " 1.5267119244391971,\n",
       " 0.8863218946350894,\n",
       " 0.8536372559712073,\n",
       " 0.7242877219879903,\n",
       " 1.4429352619509614,\n",
       " 1.1787286063569682,\n",
       " 0.4763534536403236,\n",
       " 1.9216198563030702,\n",
       " 1.8420575572173128,\n",
       " 0.4851765284782876,\n",
       " 0.6691176470588235,\n",
       " 3.6750663129973473,\n",
       " 0.622054956952106,\n",
       " 0.6834079230896735,\n",
       " 0.7683725442638856,\n",
       " 2.0525568181818183,\n",
       " 0.34202229911572474,\n",
       " 0.8790467625899281,\n",
       " 1.9127877237851665,\n",
       " 0.9824842893434952,\n",
       " 0.8762928688078389,\n",
       " 0.7246466842677435,\n",
       " 0.9477068152593228,\n",
       " 2.4036635006784257,\n",
       " 0.7825759713990027,\n",
       " 0.3490021639817264,\n",
       " 1.1605235962797107,\n",
       " 0.9590382902938558,\n",
       " 0.8285360569483132,\n",
       " 0.8389988793425476,\n",
       " 0.6006233303650935,\n",
       " 2.892512972572276,\n",
       " 0.6432598667350077,\n",
       " 0.6625498007968128,\n",
       " 1.6746843054720384,\n",
       " 0.7899461400359067,\n",
       " 0.6095454545454545,\n",
       " 1.68530947054437,\n",
       " 2.021681415929204,\n",
       " 0.35675202451302257,\n",
       " 2.8650306748466257,\n",
       " 1.1490364025695932,\n",
       " 0.33656354826686546,\n",
       " 1.6489479512735328,\n",
       " 0.8415043653458697,\n",
       " 0.4632881085395052,\n",
       " 2.768303186907838,\n",
       " 1.3067828251400124,\n",
       " 0.5119047619047619,\n",
       " 0.7255813953488373,\n",
       " 1.1596153846153847,\n",
       " 0.7893864013266998,\n",
       " 1.3277310924369747,\n",
       " 1.0622362869198312,\n",
       " 3.1484607745779543,\n",
       " 0.6459548967039899,\n",
       " 1.040283203125,\n",
       " 0.6531330673550809,\n",
       " 0.9044196909809558,\n",
       " 0.4549066348827971,\n",
       " 4.063755458515283,\n",
       " 0.8201160541586073,\n",
       " 0.4554507337526205,\n",
       " 1.1317606444188724,\n",
       " 1.507371631926792,\n",
       " 0.3086003372681282,\n",
       " 1.360655737704918,\n",
       " 0.8987951807228916,\n",
       " 0.6979445933869526,\n",
       " 6.651728553137004,\n",
       " 0.465255052935515,\n",
       " 0.7852709971038477,\n",
       " 0.5948366701791359,\n",
       " 4.3038086802480064,\n",
       " 0.6077382177402758,\n",
       " 0.3636979343040975,\n",
       " 0.7579143389199255,\n",
       " 1.6695331695331694,\n",
       " 0.7652685798381162,\n",
       " 0.7865384615384615,\n",
       " 1.5929095354523228,\n",
       " 0.7996930161166539,\n",
       " 0.4376199616122841,\n",
       " 1.4254385964912282,\n",
       " 5.273846153846153,\n",
       " 1.5568844807467912,\n",
       " 0.2844294547498595,\n",
       " 1.7299077733860342,\n",
       " 0.8724295506473724,\n",
       " 1.1562636403317328,\n",
       " 0.6949792374480936,\n",
       " 10.105920695274307,\n",
       " 0.21913464122547704,\n",
       " 0.3465783664459161,\n",
       " 1.191790516631281,\n",
       " 0.588479809976247,\n",
       " 1.6498486377396568,\n",
       " 1.0415902140672784,\n",
       " 0.4051673517322372,\n",
       " 3.1536231884057973,\n",
       " 0.96875,\n",
       " 0.6176470588235294,\n",
       " 0.9178187403993856,\n",
       " 0.9255230125523012,\n",
       " 1.2920433996383365,\n",
       " 1.4688593421973408,\n",
       " 2.3911386374464034,\n",
       " 0.26319984060569834,\n",
       " 0.9742619227857684,\n",
       " 1.6907536907536909,\n",
       " 1.4145220588235294,\n",
       " 0.6994801819363223,\n",
       " 3.3590339061774266,\n",
       " 0.903899336283186,\n",
       " 1.5852837693131403,\n",
       " 0.5329537778635529,\n",
       " 0.4959261271048343,\n",
       " 2.3161737860533043,\n",
       " 0.6439155107187894,\n",
       " 1.0193390452876376,\n",
       " 0.7572046109510087,\n",
       " 3.393910561370124,\n",
       " 0.5808802915615363,\n",
       " 0.4903474903474904,\n",
       " 2.8904199475065617,\n",
       " 1.1590238365493757,\n",
       " 0.6570365292331799,\n",
       " 0.8978983455060366,\n",
       " 0.7888446215139442,\n",
       " 1.845959595959596,\n",
       " 0.5623575011399908,\n",
       " 1.9724305696330835,\n",
       " 0.6409044193216855,\n",
       " 0.5902822322001283,\n",
       " 2.3227383863080684,\n",
       " 1.0208187134502924,\n",
       " 0.8012144821264895,\n",
       " 1.4767624767624767,\n",
       " 0.9153674832962138,\n",
       " 0.658732677456892,\n",
       " 0.4231572185643167,\n",
       " 2.0755218216318787,\n",
       " 0.5066739806180289,\n",
       " 0.6225189462287982,\n",
       " 2.060289855072464,\n",
       " 1.2886888013505908,\n",
       " 0.584061135371179,\n",
       " 0.5222429906542057,\n",
       " 0.9778095919828204,\n",
       " 2.2547584187408494,\n",
       " 0.7977272727272727,\n",
       " 0.46398046398046394,\n",
       " 1.6570175438596493,\n",
       " 0.6431974589730016,\n",
       " 1.2748971193415637,\n",
       " 0.4170432537120723,\n",
       " 1.405572755417957,\n",
       " 0.9107929515418502,\n",
       " 1.5949214026602176,\n",
       " 0.7808946171341926,\n",
       " 4.956310679611652,\n",
       " 0.3308521057786484,\n",
       " 0.9034931912374186,\n",
       " 0.9036697247706422,\n",
       " 3.5917331399564905,\n",
       " 0.5491621239652735,\n",
       " 1.008823529411765,\n",
       " 0.8327259475218659,\n",
       " 0.8582056892778993,\n",
       " 1.4487506374298826,\n",
       " 0.7965505103836678,\n",
       " 2.7909854175872737,\n",
       " 0.3130145661811273,\n",
       " 0.6019221041982802,\n",
       " 2.760504201680672,\n",
       " 0.5488584474885845,\n",
       " 2.2567942318358294,\n",
       " 0.6461046940280167,\n",
       " 0.6550019018638266,\n",
       " 1.0017421602787455,\n",
       " 2.2081159420289853,\n",
       " 2.002887897085849,\n",
       " 0.26097784768645954,\n",
       " 0.5760924158714213,\n",
       " 1.4350479511769834,\n",
       " 2.7818955042527342,\n",
       " 1.1600786197859794,\n",
       " 0.4710090361445783,\n",
       " 0.9420463629096724,\n",
       " 8.508273228680526,\n",
       " 0.35339583125560986,\n",
       " 0.6358120502328206,\n",
       " 0.9494007989347536,\n",
       " 0.7353903693314633,\n",
       " 0.5276541640178004,\n",
       " 1.2722891566265058,\n",
       " 1.2045454545454546,\n",
       " 1.8565251572327044,\n",
       " 0.8096548803726445,\n",
       " 0.8049163179916318,\n",
       " 0.98635477582846,\n",
       " 0.7467061923583662,\n",
       " 0.7176885752095279,\n",
       " 0.8672403196066379,\n",
       " 1.1197732104890148,\n",
       " 1.8968354430379744,\n",
       " 1.7681014347681014,\n",
       " 0.7527835440649179,\n",
       " 0.8922035597894209,\n",
       " 1.0266928912615902,\n",
       " 2.7274220032840724,\n",
       " 0.5937186433875176,\n",
       " 0.5587290856853135,\n",
       " 0.4358741681790684,\n",
       " 5.920194309507287,\n",
       " 0.6804595006447075,\n",
       " 0.5991386735572782,\n",
       " 0.6814261069580219,\n",
       " 1.940928270042194,\n",
       " 0.6323913043478261,\n",
       " 0.6565830182193193,\n",
       " 0.9968586387434556,\n",
       " 1.5913865546218486,\n",
       " 0.8693069306930693,\n",
       " 1.5611237661351556,\n",
       " 0.9097762645914396,\n",
       " 0.44693932103715583,\n",
       " 2.8744019138755985,\n",
       " 0.6227632126508531,\n",
       " 1.3337788172402272,\n",
       " 0.6896292585170339,\n",
       " 0.523065746458409,\n",
       " 0.5708333333333333,\n",
       " 14.538929440389294,\n",
       " 0.945443895908292,\n",
       " 0.2946278431719621,\n",
       " 1.1297686993091018,\n",
       " 2.482850305769742,\n",
       " 0.8819875776397516,\n",
       " 0.4731665857212239,\n",
       " 0.4801129073646394,\n",
       " 1.8252271512560128,\n",
       " 2.641581259150805,\n",
       " 0.2639396962642723,\n",
       " 1.014279714405712,\n",
       " 0.9333333333333332,\n",
       " 2.3646850044365566,\n",
       " 0.41613508442776737,\n",
       " 2.0432822362488734,\n",
       " 3.233671668137688,\n",
       " 0.3806209484817468,\n",
       " 0.6376837576192184,\n",
       " 0.6289007590666292,\n",
       " 1.1573535985695127,\n",
       " 0.9242950946311316,\n",
       " 0.5775177601337234,\n",
       " 3.0969609261939217,\n",
       " 1.0406542056074766,\n",
       " 0.5817242927705434,\n",
       " 0.9305287533770744,\n",
       " 0.6184155951887184,\n",
       " 1.1140174379611,\n",
       " 1.7796508127633957,\n",
       " 0.9705683355886332,\n",
       " 0.4778668525618682,\n",
       " 0.9080962800875274,\n",
       " 0.7799196787148595,\n",
       " 2.066941297631308,\n",
       " 0.9048330842052816,\n",
       " 1.8381057268722467,\n",
       " 0.5092869982025164,\n",
       " 1.128235294117647,\n",
       " 1.2867570385818559,\n",
       " 0.4282820097244733,\n",
       " 1.6168401135288553,\n",
       " 1.2480983031012287,\n",
       " 0.10126582278481013,\n",
       " 11.092592592592592,\n",
       " 1.6197829716193657,\n",
       " 2.093532594692089,\n",
       " 0.5726769230769231,\n",
       " 3.11197077154524,\n",
       " 0.29461325966850826,\n",
       " 1.3776371308016877,\n",
       " 0.701888718734048,\n",
       " 0.7856969696969697,\n",
       " 2.456957729095958,\n",
       " 1.2087153083008917,\n",
       " 1.1588571428571428,\n",
       " 2.124350008965393,\n",
       " 0.32019413378349865,\n",
       " 1.2162910241202056,\n",
       " 0.8140442132639792,\n",
       " 0.8111022364217252,\n",
       " 1.7664533070736912,\n",
       " 0.2512310694044411,\n",
       " 1.343195266272189,\n",
       " 0.6855726872246696,\n",
       " 2.084738955823293,\n",
       " 0.6680793681371605,\n",
       " 0.6756055363321799,\n",
       " 0.9995731967562952,\n",
       " 1.7860802732707088,\n",
       " 0.9921109251733206,\n",
       " 1.1819277108433734,\n",
       " 0.3141692150866463,\n",
       " 0.536664503569111,\n",
       " 5.511487303506651,\n",
       " 0.5616498464238702,\n",
       " 0.68828125,\n",
       " 1.2894438138478999,\n",
       " 1.0550176056338028,\n",
       " 0.5398414685022945,\n",
       " 1.5757341576506956,\n",
       " 0.6566944580676802,\n",
       " 2.2285287528005973,\n",
       " 0.7768096514745308,\n",
       " 1.1039689387402931,\n",
       " 1.677999218444705,\n",
       " 1.979040521658128,\n",
       " 0.4052718286655684,\n",
       " 1.4111498257839719,\n",
       " 0.7786008230452675,\n",
       " 1.944767441860465,\n",
       " 0.6543008560945781,\n",
       " 0.7198338525441329,\n",
       " 1.0147143681477206,\n",
       " 0.5294284901905033,\n",
       " 0.7722878625134264,\n",
       " 9.625173852573019,\n",
       " 0.7125207716205476,\n",
       " 0.5043601703508416,\n",
       " 0.6578206674708484,\n",
       " 4.537591687041565,\n",
       " 0.6344716104263488,\n",
       " 0.8217622080679405,\n",
       " 1.1741377083064204,\n",
       " 0.8907470568819452,\n",
       " 1.5526185770750989,\n",
       " 1.9011137629276051,\n",
       " 0.4160773318826631,\n",
       " 0.6898320426430655,\n",
       " 0.8289838168829275,\n",
       " 1.2996834329933171,\n",
       " 0.8031123139377537,\n",
       " 0.6424599831508003,\n",
       " 1.4933123524783636,\n",
       " 1.1155602388479098,\n",
       " 0.8674433249370277,\n",
       " 0.8005444646098003,\n",
       " 1.5214237134436637,\n",
       " 1.7358068842199377,\n",
       " 0.3871576959395656,\n",
       " 0.9791574279379156,\n",
       " 0.7502264492753623,\n",
       " 1.4805312405674615,\n",
       " 2.4468909276248727,\n",
       " 1.3559406765539077,\n",
       " 0.3479783704067838,\n",
       " 0.8793925481193714,\n",
       " 1.2337349397590365,\n",
       " 0.5299479166666666,\n",
       " 1.3372235872235871,\n",
       " 1.181901699586587,\n",
       " 0.719782355227361,\n",
       " 3.1843952483801297,\n",
       " 0.4275540483255617,\n",
       " 2.020820939916716,\n",
       " 1.3875968992248062,\n",
       " 0.371826603493388,\n",
       " 0.9828832255610498,\n",
       " 0.5092879256965944,\n",
       " 2.0113981762917934,\n",
       " 0.623536078579524,\n",
       " 0.7209936382914268,\n",
       " 0.9382352941176472,\n",
       " 1.025974025974026,\n",
       " 0.7525098210388477,\n",
       " 0.5864269141531323,\n",
       " 2.0237388724035608,\n",
       " 0.8128054740957967,\n",
       " 1.772699939867709,\n",
       " 5.183853459972863,\n",
       " 0.3566287135191729,\n",
       " 0.4928440366972477,\n",
       " 2.6005212211466864,\n",
       " 0.446957766642806,\n",
       " 0.7351057014734145,\n",
       " 1.0326797385620916,\n",
       " 0.8451476793248945,\n",
       " 1.166250624063904,\n",
       " 1.7893835616438356,\n",
       " 1.321531100478469,\n",
       " 1.2518102824040551,\n",
       " 0.6112798264642082,\n",
       " 0.5327655547669742,\n",
       " 1.757548845470693,\n",
       " 0.6293582617483577,\n",
       " 0.60457647531112,\n",
       " 1.6460823373173972,\n",
       " 1.7341670028237193,\n",
       " 0.8504303326354966,\n",
       " 3.228391684901532,\n",
       " 0.548928238583411,\n",
       " 0.6050316406852909,\n",
       " 0.5867346938775511,\n",
       " 0.7726086956521739,\n",
       " 0.8165447383230163,\n",
       " 1.4975878704341834,\n",
       " 1.0395766221813159,\n",
       " 2.8906595838866758,\n",
       " 0.7241960183767229,\n",
       " 0.45760203002749,\n",
       " 0.8193160813308688,\n",
       " 1.0998307952622672,\n",
       " 0.5312820512820513,\n",
       " 1.6216216216216215,\n",
       " 1.1803571428571429,\n",
       " 0.6111951588502269,\n",
       " 1.1625412541254123,\n",
       " 2.2342086586231367,\n",
       " 0.4189961880559085,\n",
       " 0.5777103866565579,\n",
       " 1.4055118110236222,\n",
       " 1.8347338935574231,\n",
       " 0.9374045801526718,\n",
       " 0.4695982627578719,\n",
       " 1.8820809248554915,\n",
       " 0.6320638820638821,\n",
       " 2.0194363459669584,\n",
       " 0.9937439846005774,\n",
       " 0.4222760290556901,\n",
       " 0.9621559633027524,\n",
       " 3.8379022646007153,\n",
       " 0.3527950310559006,\n",
       " 1.9973591549295773,\n",
       " 0.2635522256500661,\n",
       " 9.637123745819398,\n",
       " 0.640117994100295,\n",
       " 1.8208186500406611,\n",
       " 0.4120887300878368,\n",
       " 0.45014450867052025,\n",
       " 0.6388443017656501,\n",
       " 2.129396984924623,\n",
       " 4.202949852507373,\n",
       " 0.562605277933745,\n",
       " 0.5506487025948104,\n",
       " 5.575894879927503,\n",
       " 0.7606045831301804,\n",
       " 0.513568376068376,\n",
       " 0.5941335552319534,\n",
       " 1.6922268907563025,\n",
       " 0.9915166563211256,\n",
       " 1.1464941569282137,\n",
       " 0.6869311976701856,\n",
       " 0.7493375728669847,\n",
       " 1.2365629420084865,\n",
       " 1.1724335144409497,\n",
       " 1.8004878048780488,\n",
       " 7.25304795448388,\n",
       " 0.4462291285346083,\n",
       " 1.2494977398292315,\n",
       " 0.498542860014069,\n",
       " 0.7595914802123228,\n",
       " 0.9198584697036708,\n",
       " 1.147129531685739,\n",
       " 0.7731578506161455,\n",
       " 0.8252195597961618,\n",
       " 1.1340165549862042,\n",
       " 0.764801297648013,\n",
       " 0.7751855779427359,\n",
       " 0.923001758843072,\n",
       " 0.8157950455219141,\n",
       " 1.5660524266805087,\n",
       " 2.3740470666224724,\n",
       " 0.45717277486911,\n",
       " 1.0062604977859215,\n",
       " 0.8487101669195751,\n",
       " 1.924548542821384,\n",
       " 0.6842251950947603,\n",
       " 1.2351663272233535,\n",
       " 0.38320325381994064,\n",
       " 0.8126792885829031,\n",
       " 0.7867984468761031,\n",
       " 2.6496186630776135,\n",
       " 0.8103623433796141,\n",
       " 0.5672795653990806,\n",
       " 1.3149171270718232,\n",
       " 0.9075630252100841,\n",
       " 1.066049382716049,\n",
       " 1.3236826867400115,\n",
       " 0.6100174978127734,\n",
       " 1.882395123700251,\n",
       " 0.6965714285714286,\n",
       " 1.0347279190593384,\n",
       " 0.8033826638477801,\n",
       " 1.125657894736842,\n",
       " 0.7729398012857978,\n",
       " 0.5584120982986768,\n",
       " 1.7447528774542993,\n",
       " 1.1358168412883198,\n",
       " 0.8308848650495387,\n",
       " 0.32648026315789475,\n",
       " 2.472292191435768,\n",
       " 0.7213448802852777,\n",
       " 2.3121468926553668,\n",
       " 0.7040317654245571,\n",
       " 0.7678958785249458,\n",
       " 1.0062146892655368,\n",
       " 1.4609769792251543,\n",
       " 0.8116833205226749,\n",
       " 2.203125,\n",
       " 0.9778637438211908,\n",
       " 1.0813186813186813,\n",
       " 1.0676829268292682,\n",
       " 0.5640586331620027,\n",
       " 1.3155585555180558,\n",
       " 0.4476654694715239,\n",
       " 2.0916905444126077,\n",
       " 1.2071232876712328,\n",
       " 0.4791193826600091,\n",
       " 0.8839412600663192,\n",
       " 0.994640943193998,\n",
       " 0.6950431034482759,\n",
       " 1.4705426356589146,\n",
       " 1.224565102793885,\n",
       " 0.6173052087817478,\n",
       " 1.6499302649930263,\n",
       " 0.6318681318681318,\n",
       " 1.02876254180602,\n",
       " 1.3777633289986997,\n",
       " 0.8961774421897122,\n",
       " 0.7193259610321222,\n",
       " 1.3169838945827232,\n",
       " 5.622568093385214,\n",
       " 1.3041028175976273,\n",
       " 0.4640285042832234,\n",
       " 1.076131351086424,\n",
       " 0.7151965993623804,\n",
       " 0.8768838887709616,\n",
       " 0.8370854514645364,\n",
       " 1.3473105841526891,\n",
       " 0.6254561064606139,\n",
       " 2.531571722717913,\n",
       " 0.8779991866612444,\n",
       " 1.2164582368380423,\n",
       " 0.5330625713923086,\n",
       " 0.5409523809523811,\n",
       " 2.234154929577465,\n",
       " 1.5656028368794326,\n",
       " 0.9233673084182712,\n",
       " 0.4745162169528482,\n",
       " 0.6711659965537048,\n",
       " 0.8703465982028241,\n",
       " 1.8633235004916424,\n",
       " 1.1680738786279683,\n",
       " 0.7343573526089903,\n",
       " 0.9806213472777608,\n",
       " 1.0043914680050188,\n",
       " 1.5893191755153029,\n",
       " 1.3727647867950479,\n",
       " 1.4626395648439736,\n",
       " 0.5595028381287923,\n",
       " 2.0850096204302955,\n",
       " 0.7918624161073825,\n",
       " 0.48871702510859205,\n",
       " 0.5443312378061999,\n",
       " 1.0760653126244524,\n",
       " 1.8767579570688382,\n",
       " 0.8398737921514494,\n",
       " 1.1465132660248885,\n",
       " 2.5988122056113045,\n",
       " 0.7535066981875492,\n",
       " 0.5153733528550513,\n",
       " 1.2128652597402598,\n",
       " 0.5111259829345826,\n",
       " 1.2713584288052373,\n",
       " 0.9660144181256436,\n",
       " 1.5349147121535183,\n",
       " 1.110088557041153,\n",
       " 0.7343969967151572,\n",
       " 1.3623003194888177,\n",
       " 1.5847404627892432,\n",
       " 0.5041436464088398,\n",
       " 1.0906066536203525,\n",
       " 1.5872958909025658,\n",
       " 0.6224282161428895,\n",
       " 0.5170722847802397,\n",
       " 1.4769933263083947,\n",
       " 3.024970273483948,\n",
       " 1.7811320754716982,\n",
       " 1.1243820621468927,\n",
       " 1.1218497291355891,\n",
       " 0.5688291692910631,\n",
       " 2.2284694881889764,\n",
       " 0.2578810798873737,\n",
       " 0.8433954185399272,\n",
       " 0.7233151415154208,\n",
       " 1.2825057027548692,\n",
       " 1.0080722397044741,\n",
       " 0.7590933767643865,\n",
       " 0.9190059002324334,\n",
       " 0.9439688715953308,\n",
       " 0.9907254740313272,\n",
       " 0.913459538173497,\n",
       " 1.5165110453199726,\n",
       " 0.5834209340741853,\n",
       " 0.8471042471042471,\n",
       " 0.7645092676997873,\n",
       " 1.2118441971383147,\n",
       " 0.8645457527058051,\n",
       " 1.6361911987860394,\n",
       " 0.6962670994667285,\n",
       " 0.4948384948384949,\n",
       " 2.6890982503364738,\n",
       " 0.9024024024024024,\n",
       " 1.120077648363838,\n",
       " 0.7308739787076008,\n",
       " 1.0819783197831978,\n",
       " 0.7564182842830307,\n",
       " 0.3712748344370861,\n",
       " 2.0390189520624302,\n",
       " 3.112629852378349,\n",
       " 1.4812928157386265,\n",
       " 1.1922210364046009,\n",
       " 0.29898547841655065,\n",
       " 1.4105123087159015,\n",
       " 0.4721698113207547,\n",
       " 1.0689310689310692,\n",
       " 2.825233644859813,\n",
       " 1.0157128680119087,\n",
       " 1.5613092330239375,\n",
       " 0.5319148936170213,\n",
       " 0.6643137254901961,\n",
       " 1.1573199527744982,\n",
       " 0.6812037745473094,\n",
       " 0.852864095844253,\n",
       " 1.2418788410886743,\n",
       " 0.7794273594909862,\n",
       " 2.866213151927438,\n",
       " 0.4272151898734177,\n",
       " 1.4585185185185183,\n",
       " 3.6937531742001015,\n",
       " 0.4307713460745222,\n",
       " 0.6144270667092244,\n",
       " 1.1825974025974026,\n",
       " 1.1074017131561609,\n",
       " 0.6408171360571202,\n",
       " 0.8944599195295574,\n",
       " 4.309688581314879,\n",
       " 0.5621838619028503,\n",
       " 0.6215367037989146,\n",
       " 0.6684283088235294,\n",
       " 1.375730491577862,\n",
       " 1.7098950524737633,\n",
       " 0.7780213356714891,\n",
       " 1.053343350864012,\n",
       " 0.9012125534950072,\n",
       " 0.629996042738425,\n",
       " 0.8410804020100503,\n",
       " 0.7300224047796864,\n",
       " 3.41687979539642,\n",
       " 0.6643712574850299,\n",
       " 1.0042812077512393,\n",
       " 0.3580884002692394,\n",
       " 1.6058897243107768,\n",
       " 1.237612173234491,\n",
       " 3.92749054224464,\n",
       " 1.2474715042542943,\n",
       " 0.6183643266199086,\n",
       " 1.1729448491155048,\n",
       " 0.5067423704755145,\n",
       " 0.751750700280112,\n",
       " 0.7286911970190965,\n",
       " 1.1313518696069031,\n",
       " 0.5836158192090396,\n",
       " 0.8649564375605033,\n",
       " 1.2747621712367097,\n",
       " 1.697102721685689,\n",
       " 1.8039317123642007,\n",
       " 0.6808144536851162,\n",
       " 1.443133951137321,\n",
       " 1.1554290718038531,\n",
       " 1.6155109258557534,\n",
       " 2.8752150117279123,\n",
       " 0.492195572959156,\n",
       " 0.9332596685082872,\n",
       " 0.4847856973715368,\n",
       " 1.6076444010257662,\n",
       " 0.8484618306114698,\n",
       " 0.4136078782452999,\n",
       " 1.105844155844156,\n",
       " 1.174202387942846,\n",
       " 1.6861143523920654,\n",
       " 0.7168561542263965,\n",
       " 0.9969659357330024,\n",
       " 1.8304053119380277,\n",
       " 0.910217654171705,\n",
       " 0.6249584855529724,\n",
       " 1.014879766175103,\n",
       " 1.2501636339835056,\n",
       " 1.0583246073298431,\n",
       " 1.0744038785000494,\n",
       " 0.4440556220646468,\n",
       " 1.3484031522189963,\n",
       " 0.9967702245462936,\n",
       " 1.6232062953247957,\n",
       " 0.4106463878326996,\n",
       " 1.2923611111111108,\n",
       " 0.7497761060361813,\n",
       " 0.9197324414715721,\n",
       " 0.6290909090909091,\n",
       " 2.102394715111478,\n",
       " 1.9277297721916733,\n",
       " 0.6557660961695192,\n",
       " 0.9454714929314898,\n",
       " 0.5961222477817942,\n",
       " 1.7772877618522602,\n",
       " 0.4692928039702233,\n",
       " 2.4421678783873104,\n",
       " 0.8683355886332882,\n",
       " 0.521894966495247,\n",
       " 0.9447596297402209,\n",
       " 0.8268015170670038,\n",
       " 1.2377675840978593,\n",
       " 1.0682520074119828,\n",
       " 1.058687481931194,\n",
       " 0.3050245767340251,\n",
       " 3.8907788719785135,\n",
       " 0.5398067188219052,\n",
       " 1.6112531969309465,\n",
       " 1.1187830687830689,\n",
       " 0.4450224639394656,\n",
       " 2.468119022316684,\n",
       " 1.544886975242196,\n",
       " 1.5121237458193981,\n",
       " 1.2805271403557277,\n",
       " 0.5242893127024111,\n",
       " 1.9096774193548387,\n",
       " 0.3998706152961472,\n",
       " 0.7362933668883695,\n",
       " 0.81298828125,\n",
       " 0.9327327327327328,\n",
       " 2.294913071474565,\n",
       " 0.9812008978675644,\n",
       " 0.2720903631684301,\n",
       " 1.742511823436679,\n",
       " 1.2949336550060313,\n",
       " 0.5887284583139264,\n",
       " 1.4675632911392404,\n",
       " 0.9067385444743936,\n",
       " 0.6950059453032105,\n",
       " 0.6958939264328486,\n",
       " 1.6588813767670558,\n",
       " 1.031493145609485,\n",
       " 0.8329741379310345,\n",
       " 1.0232858990944371,\n",
       " 0.6746734091866835,\n",
       " 0.7282948157401624,\n",
       " 7.680960548885078,\n",
       " 0.2845020098258151,\n",
       " 0.5529827315541601,\n",
       " 1.7331440738112136,\n",
       " 2.592137592137592,\n",
       " 1.0233807266982622,\n",
       " 0.7008335906143871,\n",
       " 0.7224669603524229,\n",
       " 1.1466463414634147,\n",
       " 0.6660462642914119,\n",
       " 2.3273453093812377,\n",
       " 1.106689536878216,\n",
       " ...]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(test_data.iloc[:, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "factor[\"test1\"] = test_data.iloc[:, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>test1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.521915</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.746168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.711134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.229436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.756859</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.701281</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1.261562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.427706</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1.591357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.908721</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1.099950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0.607245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0.975143</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1.373439</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0.888827</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>2.685529</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.625846</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0.557212</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>1.109253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0.561005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>1.085632</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>1.197030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>0.947339</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>1.213620</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>0.600000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>1.140288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>1.003155</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>1.705800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>0.920729</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>1.543938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158891</th>\n",
       "      <td>0.978118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158892</th>\n",
       "      <td>0.928092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158893</th>\n",
       "      <td>1.452824</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158894</th>\n",
       "      <td>0.828869</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158895</th>\n",
       "      <td>0.842436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158896</th>\n",
       "      <td>1.101494</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158897</th>\n",
       "      <td>0.661941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158898</th>\n",
       "      <td>2.271881</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158899</th>\n",
       "      <td>0.878893</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158900</th>\n",
       "      <td>1.033341</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158901</th>\n",
       "      <td>0.797383</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158902</th>\n",
       "      <td>2.364177</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158903</th>\n",
       "      <td>1.336445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158904</th>\n",
       "      <td>1.271001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158905</th>\n",
       "      <td>0.440178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158906</th>\n",
       "      <td>0.923323</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158907</th>\n",
       "      <td>1.105236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158908</th>\n",
       "      <td>1.265255</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158909</th>\n",
       "      <td>0.627077</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158910</th>\n",
       "      <td>0.786081</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158911</th>\n",
       "      <td>0.847586</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158912</th>\n",
       "      <td>1.080415</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158913</th>\n",
       "      <td>1.652843</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158914</th>\n",
       "      <td>1.006647</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158915</th>\n",
       "      <td>1.902443</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158916</th>\n",
       "      <td>0.638438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158917</th>\n",
       "      <td>1.009242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158918</th>\n",
       "      <td>0.951252</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158919</th>\n",
       "      <td>1.796433</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158920</th>\n",
       "      <td>1.253349</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>158921 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           test1\n",
       "0       0.521915\n",
       "1       1.746168\n",
       "2       0.711134\n",
       "3       1.229436\n",
       "4       0.756859\n",
       "5       0.701281\n",
       "6       1.261562\n",
       "7       0.427706\n",
       "8       1.591357\n",
       "9       0.908721\n",
       "10      1.099950\n",
       "11      0.607245\n",
       "12      0.975143\n",
       "13      1.373439\n",
       "14      0.888827\n",
       "15      2.685529\n",
       "16      0.625846\n",
       "17      0.557212\n",
       "18      1.109253\n",
       "19      0.561005\n",
       "20      1.085632\n",
       "21      1.197030\n",
       "22      0.947339\n",
       "23      1.213620\n",
       "24      0.600000\n",
       "25      1.140288\n",
       "26      1.003155\n",
       "27      1.705800\n",
       "28      0.920729\n",
       "29      1.543938\n",
       "...          ...\n",
       "158891  0.978118\n",
       "158892  0.928092\n",
       "158893  1.452824\n",
       "158894  0.828869\n",
       "158895  0.842436\n",
       "158896  1.101494\n",
       "158897  0.661941\n",
       "158898  2.271881\n",
       "158899  0.878893\n",
       "158900  1.033341\n",
       "158901  0.797383\n",
       "158902  2.364177\n",
       "158903  1.336445\n",
       "158904  1.271001\n",
       "158905  0.440178\n",
       "158906  0.923323\n",
       "158907  1.105236\n",
       "158908  1.265255\n",
       "158909  0.627077\n",
       "158910  0.786081\n",
       "158911  0.847586\n",
       "158912  1.080415\n",
       "158913  1.652843\n",
       "158914  1.006647\n",
       "158915  1.902443\n",
       "158916  0.638438\n",
       "158917  1.009242\n",
       "158918  0.951252\n",
       "158919  1.796433\n",
       "158920  1.253349\n",
       "\n",
       "[158921 rows x 1 columns]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scoop import futures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "from deap import algorithms, tools, creator, base\n",
    "import random\n",
    "import operator\n",
    "\n",
    "\n",
    "creator.create(\"FitnessMin\", base.Fitness, weights=(-1.0, ))\n",
    "creator.create(\"Individual\", list, fitness=creator.FitnessMin)\n",
    "\n",
    "toolbox = base.Toolbox()\n",
    "toolbox.register(\"attr_float\", random.random)\n",
    "toolbox.register(\"individual\", tools.initRepeat, creator.Individual, toolbox.attr_float, n=10)\n",
    "toolbox.register(\"population\", tools.initRepeat, list, toolbox.individual)\n",
    "\n",
    "\n",
    "def evaluate(individual):\n",
    "    a = sum(individual)\n",
    "    b = len(individual)\n",
    "    return a / b,\n",
    "\n",
    "\n",
    "toolbox.register(\"mate\", tools.cxTwoPoint)\n",
    "toolbox.register(\"mutate\", tools.mutGaussian, mu=0, sigma=1, indpb=0.2)\n",
    "toolbox.register(\"select\", tools.selTournament, tournsize=3)\n",
    "toolbox.register(\"evaluate\", evaluate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "toolbox.map?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "evol_process_record.json\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
